import { Component, EventEmitter, OnInit, Output } from "@angular/core";
import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
import { NzMessageService } from "ng-zorro-antd/message";
import { DeviceService } from "../device.service";
import { DeviceGroupService } from "../../device-group-list/device-group.service";
import { UserService } from "../../../pms/user-list/user.service";
import { NzTreeNode } from "ng-zorro-antd/tree";

@Component({
  selector: 'app-device-scrap',
  templateUrl: './device-scrap.component.html',
  styles: [
  ]
})
export class DeviceScrapComponent implements OnInit {


  constructor(private formBuilder: FormBuilder,
              private msgSrv: NzMessageService,
              private deviceSrv: DeviceService,
              private deviceGroupSrv: DeviceGroupService,
              private userSrv: UserService,
  ) {
  }

  @Output() voted = new EventEmitter();

  validateForm!: FormGroup;

  deviceGroups = [];
  // 责任人信息
  users = [];

  dateFormat = 'yyyy-MM-dd';
  visible = false;

  isUpdate = false;

  edit(param) {
    this.visible = true;
    this.isUpdate = true;

    if (param) {
      this.validateForm.patchValue(param);
    } else {
      this.validateForm.reset();
    }
  }

  close(): void {
    this.visible = false;
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    // 报废提交
    this.deviceSrv.scrapped(this.validateForm.value).subscribe((res) => {
      if (res.code === 1) {
        this.msgSrv.success('保存完成');
        this.close();
        this.voted.emit();
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  updateConfirmValidator(): void {
    /** wait for refresh value */
    Promise.resolve().then(() => this.validateForm.controls.checkPassword.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return {required: true};
    } else if (control.value !== this.validateForm.controls.password.value) {
      return {confirm: true, error: true};
    }
    return {};
  };

  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

  loadDeviceGroupDate() {
    this.deviceGroupSrv.getAll().subscribe((res) => {
      if (res.code === 1) {
        this.deviceGroups = res.data;
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }


  loadUserData() {
    this.userSrv.getAll().subscribe((res) => {
      if (res.code === 1) {
        this.users = res.data;
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  ngOnInit(): void {
    // 加载所有设备组的数据
    this.loadDeviceGroupDate();
    // 加载人员数据
    this.loadUserData();

    this.validateForm = this.formBuilder.group({
      id: [null],
      // 报废原因
      scrappedReason: [null, [Validators.required]],
      // 报废日期
      scrappedDateTime: [null, [Validators.required]],
      // 报废操作人员
      scrappedUserId: [null, [Validators.required]],
      // 报废的备注
      scrappedMemo: [null],
    });

  }

  nzDisplayWith(node: NzTreeNode) {
    return node.title;
  }

  onChange($event: string): void {
    console.log($event);
  }

}
